package cn.shengchao.dubbo.api.rule;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONObject;
import cn.shengchao.examstar.rpc.wecom.api.RuleEngineApi;
import cn.shengchao.examstar.rpc.wecom.dto.RuleDict;
import cn.shengchao.examstar.rpc.wecom.dto.RuleMatchValueDict;
import cn.shengchao.qywx.infrastrure.po.QywxRuleMatchDic;
import cn.shengchao.qywx.infrastrure.service.QywxRuleMatchDicService;
import cn.shengchao.rule.base.ActionType;
import cn.shengchao.rule.condition.ConditionType;
import cn.shengchao.rule.engine.RuleElementContainer;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @program: examstarapi
 * @description: 规则引擎调用
 * @author: Xiong weiqiang
 * @create: 2025-04-11 20:54
 */
@DubboService
public class RuleEngineDubboApi implements RuleEngineApi {
    @Autowired
    private  QywxRuleMatchDicService ruleMatchDicService;
    @Override
    public List<RuleDict> getConditionTypeList() {
        return Arrays.stream(ConditionType.values()).map(a->RuleDict.builder().key(a.name()).value(a.getConditionTypeName()).build()).toList();
    }

    @Override
    public List<RuleDict> getMatchTypeList() {
        List<RuleDict> matchTypeList = new ArrayList<>();
        RuleElementContainer.matchTypeConcurrentHashMap.forEach((k,v)->{
            RuleDict ruleDict=RuleDict.builder().key(k).value(v.getMatchTypeValue()).build();
            matchTypeList.add(ruleDict);
        });
        return matchTypeList;
    }

    @Override
    public List<RuleDict> getValueTypeList() {
        List<RuleDict> valueTypeList = new ArrayList<>();
        RuleElementContainer.valueTypeConcurrentHashMap.values().forEach(a->{
            RuleDict ruleDict=RuleDict.builder().key(a.valueTypeKey()).value(a.valueTypeName()).build();
            valueTypeList.add(ruleDict);
        });
        return valueTypeList;
    }

    @Override
    public List<RuleDict> getActionTypeList() {
        return Arrays.stream(ActionType.values()).map(a->RuleDict.builder().key(a.name()).value(a.getActionTypeName()).build()).toList();
    }

    @Override
    public List<RuleDict> getConditionList(String conditionType) {
        List<RuleDict> conditionList = new ArrayList<>();
        RuleElementContainer.conditionParamKeyConcurrentHashMap.values().forEach(a->{
            if(a.getConditionType().equals(conditionType)){
                String iValueTypeKey=a.getValueTypeKey();
                JSONObject jsonObject=new JSONObject();
                jsonObject.set("valueTypeKey",iValueTypeKey);
                jsonObject.set("matchTypeKeys",a.getMatchTypeKeys());
                RuleDict ruleDict=RuleDict.builder().key(a.getParamKey()).value(a.getParamShow()).addition(jsonObject).build();
                conditionList.add(ruleDict);
            }
        });
        return conditionList;
    }

    @Override
    public List<RuleMatchValueDict> getConditionMatchValueListByConditionParamKey(String conditionParamKey) {
        List<QywxRuleMatchDic> matchDicList=ruleMatchDicService.list(Wrappers.lambdaQuery(QywxRuleMatchDic.class)
                .eq(QywxRuleMatchDic::getConditionParamKey,conditionParamKey)
        );
        if (CollUtil.isNotEmpty(matchDicList)){
            return matchDicList.stream().map(a->RuleMatchValueDict.builder()
                    .conditionParamKey(a.getConditionParamKey())
                    .valueTypeKey(a.getValueTypeKey())
                    .matchValue(a.getMatchValue())
                    .matchName(a.getMatchName())
                    .matchAdditionKey(a.getMatchAdditionKey())
                    .build()).toList();
        }
        return List.of();
    }
}